<?php
namespace Api\Controller;
use Think\Controller;
class CouponController extends CommonController {
    public function index(){
        $map = array();
        //$map['wid'] = session('wid');
        $map['status'] = 1;
        if($_GET['openid']){
            $map['openid'] = $_GET['openid'];
        }
        if($_GET['shop_id']){
            $map['shop_id'] = $_GET['shop_id'];
        }
        $list = M("Coupon")->where($map)->order("last_time desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['buy_percent'] = ($value['buy_num']/$value['stock'])*100;
        }
        $this->ajaxReturn($list);
    }

    public function item(){
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        $info = M("Coupon")->where($map)->find();
        $info['pics'] = explode(",", $info['pics']);
        $this->ajaxReturn($info);
    }
    // 领取优惠券方法
    public function getCoupon(){
        $model = M("CouponLog");

        $new['wid'] = session('wid');
        $new['coupon_id'] = I("coupon_id");
        $new['openid'] = I("openid");

        if($model->where($new)->find()){
            $arr = array("err_code"=>400,"err_msg"=>"你已经领取过了！");
            $this->ajaxReturn($arr);
        }

        $eq['id'] = I("coupon_id");
        $coupon = M("Coupon")->where($eq)->find();
        if($coupon['buy_num'] >= $coupon['stock']){
            $arr = array("err_code"=>400,"err_msg"=>"优惠券已经被抢光了！");
            $this->ajaxReturn($arr);
        }
        $new['shop_id'] = $coupon['shop_id'];
        $new['title'] = $coupon['title'];
        $new['coupon_price'] = $coupon['coupon_price'];
        $new['coupon_remark'] = $coupon['coupon_remark'];
        $new['content'] = $coupon['content'];
        $new['img_url'] = $coupon['img_url'];
        $new['pics'] = $coupon['pics'];
        $new['create_time'] = date("Y-m-d H:i:s");
        if($model->add($new)){
            $arr = array("err_code"=>0,"err_msg"=>"领取成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }
    // 我的优惠券列表
    public function couponList(){
        $map['wid'] = session('wid');
        $map['openid'] = I('openid');
        $list = M("CouponLog")->where($map)->order("create_time desc")->select();
        $res = array("err_code"=>0,"data"=>$list, "time"=>date("Y-m-d H:i:s"));
        $this->ajaxReturn($res);
    }
    public function couponItem(){
        $map['wid'] = session('wid');
        $map['id'] = I('id');
        $map['openid'] = I('openid');
        $info = M("CouponLog")->where($map)->find();

        $eq['shop_id'] = $info['shop_id'];
        $info['shop'] = M("Shop")->where($eq)->find();
        $res = array("err_code"=>0,"data"=>$info, "time"=>date("Y-m-d H:i:s"));
        $this->ajaxReturn($res);
    }

    public function getAddForm(){
        $res['index_url'] = "/pages/mine/shop/coupon/list";

        if($_GET['id']){
            $map['id'] = $_GET['id'];
            $info = M("Coupon")->where($map)->find();
            $pics = explode(',', $info['pics']);
            $res['add_url'] = "https://www.58zltc.com/api.php/Coupon/add/id/{$_GET['id']}";

            $form[] = array('type'=>'text', 'title'=>'优惠券名称', 'name'=>'title', 'value'=>$info['title']);
            $form[] = array('type'=>'number', 'title'=>'优惠金额', 'name'=>'coupon_price', 'value'=>$info['coupon_price']);
            $form[] = array('type'=>'number', 'title'=>'发放数量', 'name'=>'stock', 'value'=>$info['stock']);
            $form[] = array('type'=>'text', 'title'=>'一句话简介', 'name'=>'coupon_remark', 'value'=>$info['coupon_remark']);
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'优惠券图片', 'name'=>'pics', 'num'=>2, 'pics'=>$pics);
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'优惠券简介', 'name'=>'content', 'value'=>$info['content']);
        }else{
            $pics = array();
            $res['add_url'] = "https://www.58zltc.com/api.php/Coupon/add";

            $form[] = array('type'=>'text', 'title'=>'优惠券名称', 'name'=>'title');
            $form[] = array('type'=>'number', 'title'=>'优惠金额', 'name'=>'coupon_price');
            $form[] = array('type'=>'number', 'title'=>'发放数量', 'name'=>'stock');
            $form[] = array('type'=>'text', 'title'=>'一句话简介', 'name'=>'coupon_remark');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'优惠券图片', 'name'=>'pics', 'num'=>2, 'pics'=>$pics);
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'优惠券简介', 'name'=>'content');
        }
        $res['form'] = $form;
        $this->ajaxReturn($res);
    }

    public function add(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        if($_GET['id']){
            $map['id'] = $_GET['id'];
            $map['wid'] = session('wid');
            $map['openid'] = $_POST['openid'];
            if(M("Coupon")->where($map)->save($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"修改成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"没有进行任何修改哦！");
                $this->ajaxReturn($arr);
            }
        }else{
            $eq['wid'] = session('wid');
            $eq['openid'] = $_POST['openid'];
            $_POST['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
            $_POST['wid'] = session('wid');
            $_POST['create_time'] = date("Y-m-d H:i:s");
            $_POST['status'] = 1;

            if(M("Coupon")->add($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
                $this->ajaxReturn($arr);
            }
        }

    }

    public function del(){
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        $map['openid'] = $_GET['openid'];

        $new['status'] = 0;
        if(M("Coupon")->where($map)->save($new)){
            $arr = array("err_code"=>0, "err_msg"=>"删除成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }
}